import fetch from 'common/src/utils/fetch';
import UTILS from "common/src/utils/utils";

/**
 * 离校学生成绩查看
 * @param {*} data
 */
function listLeaveStudentPage(data) {
    return fetch({
        url: 'api/scoremanagementservice/studentScoreQuery/listLeaveScorePage',
        method: 'post',
        data
    });
}
/**
 * 离校学生个人信息查询
 * @param {*} data  学生id
 */
function findStudentScoreByStudentId(id) {
    return fetch({
        url:
            '/api/scoremanagementservice/studentScoreQuery/findStudentScoreByStudentId?studentId=' +
            id,
        method: 'get'
    });
}

/**
 * 离校学生成绩导出
 * @param {*} data
 */
function exportLeaveScore(data) {
    const url = 'api/scoremanagementservice/studentScoreQuery/leaveScoreExport';
    let queryParams = '';
    for (let item in data) {
        if (data[item] === '') {
            continue;
        }
        queryParams += `${item}=${data[item]}&`;
    }
    queryParams = queryParams.substring(0, queryParams.lastIndexOf('&'));
    window.open(`${url}?${queryParams}`);
}

/**
 * preview student Transcript by studentId and language
 * @param {*} id
 * @param {*} lan
 */
function viewStudentScoreCard(id, lan) {
    return fetch({
        url:
            '/api/scoremanagementservice/studentScoreQuery/viewStudentScoreCard?studentId=' +
            id +
            '&language=' +
            lan,
        method: 'get'
    });
}

/**
 * export english transcript
 * @param {*} id
 * @param {*} lan
 */
function englishTranscriptExport(id) {
    const url =
        '/api/scoremanagementservice/studentScoreQuery/studentScoreCardExportWord';
    let queryParams = 'studentId=' + id + '&lan=us';
    window.open(`${url}?${queryParams}`);
}

/**
 * export chinese transcript
 * @param {*} id
 */
function chineseTranscriptExport(id) {
    const url =
        '/api/scoremanagementservice/studentScoreQuery/studentScoreCardExportWord';
    let queryParams = 'studentId=' + id + '&lan=cn';
    window.open(`${url}?${queryParams}`);
}
/**
 * 导出PDF
 */
function chineseTranscriptExportPDF(id) {
    const url = 
        '/api/scoremanagementservice/studentScoreQuery/studentScoreCardExportPdf';
    let queryParams = 'studentId=' + id + '&language=cn';
    window.open(`${url}?${queryParams}`);
}
/**
 * 导出PDF
 */
function englishTranscriptExportPDF(id) {
    const url = 
        '/api/scoremanagementservice/studentScoreQuery/studentScoreCardExportPdf';
    let queryParams = 'studentId=' + id + '&language=us';
    window.open(`${url}?${queryParams}`);
}

function batchExport(data) {
    return fetch({
        url: '/api/scoremanagementservice/studentScoreQuery/studentScoreAllExportPdf',
        method: 'post',
        responseType: 'blob',
        data
    }).then(res => {
        UTILS.downloadFile(res,data.language == 'cn' ? '中文成绩单.pdf' : '中文英文成绩单.pdf' , 'application/vnd.ms-pdf;charset=utf-8')
    })
}

function getExportProsess(key) {
    return fetch({
        url: '/api/scoremanagementservice/studentScoreQuery/exportStudentScoreZipByKey?key=' + key,
        method: 'get'
    });
}

const studentScoreOqueryForLeaveApi = {
    listLeaveStudentPage: listLeaveStudentPage,
    findStudentScoreByStudentId: findStudentScoreByStudentId,
    exportLeaveScore: exportLeaveScore,
    viewStudentScoreCard: viewStudentScoreCard,
    englishTranscriptExport: englishTranscriptExport,
    chineseTranscriptExport: chineseTranscriptExport,
    batchExport: batchExport,
    getExportProsess: getExportProsess,
    chineseTranscriptExportPDF: chineseTranscriptExportPDF,
    englishTranscriptExportPDF: englishTranscriptExportPDF
};

export default studentScoreOqueryForLeaveApi;
